package org.yaac.shared.property;

import static com.google.common.collect.Lists.newArrayList;

import java.util.Arrays;
import java.util.List;

import org.yaac.shared.egql.ResultCell;
import org.yaac.shared.egql.ResultCell.ResultCellFactory;
import org.yaac.shared.util.StringUtils;



/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class CategoryPropertyInfo extends PropertyInfo {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String payload;
	
	/**
	 * used by GWT
	 */
	@SuppressWarnings("unused")
	private CategoryPropertyInfo(){}
	
	public CategoryPropertyInfo(String payload) {
		this.payload = payload;
	}
	
	public String getPayload() {
		return payload;
	}

	@Override
	public String asHtml() {
		return payload;
	}
	
	@Override
	public int compareTo(PropertyInfo o) {
		if (o instanceof CategoryPropertyInfo) {
			return payload.compareTo(((CategoryPropertyInfo) o).payload);
		} else {
			return 0;	
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((payload == null) ? 0 : payload.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CategoryPropertyInfo other = (CategoryPropertyInfo) obj;
		if (payload == null) {
			if (other.payload != null)
				return false;
		} else if (!payload.equals(other.payload))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CategoryPropertyInfo [payload=" + payload + "]";
	}

	@Override
	public ResultCell populateResultCell(ResultCellFactory factory) {
		ResultCell cell = super.populateResultCell(factory);
		cell.setType(PropertyType.CATEGORY);
		cell.setParent(null);
		cell.setChildren(null);
		cell.setPayload(Arrays.asList(payload));
		return cell;
	}

	@Override
	public String asExpression(FilePlaceHolder placeHolder) {
		return "category(" + StringUtils.wrap(this.payload, "'") + ")";
	}
	
	@Override
	public List<String> downloadPaths() {
		return newArrayList();
	}
}
